package zhishu.iceyyy.com.bxq_2500.test;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.wang.avi.AVLoadingIndicatorView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.refreshlayout.BGANormalRefreshViewHolder;
import cn.bingoogolapple.refreshlayout.BGARefreshLayout;
import de.greenrobot.event.EventBus;
import zhishu.iceyyy.com.bxq_2500.R;
import zhishu.iceyyy.com.bxq_2500.api.API;
import zhishu.iceyyy.com.bxq_2500.api.Okhttp;
import zhishu.iceyyy.com.bxq_2500.bean.MovielistD;
import zhishu.iceyyy.com.bxq_2500.bean.MyEvent;
import zhishu.iceyyy.com.bxq_2500.myadapter.ListViewAdapter;
import zhishu.iceyyy.com.bxq_2500.utils.JSONUtils;
import zhishu.iceyyy.com.bxq_2500.utils.LogUtil;
import zhishu.iceyyy.com.bxq_2500.utils.LogUtils;
import zhishu.iceyyy.com.bxq_2500.utils.NetUtil;
import zhishu.iceyyy.com.bxq_2500.utils.SpfUtils;


/**
 * Created by 智江鹏 on 2016/11/18.
 * 处置场  已接单
 */


public class Movie_Fragment extends Base_Fragment implements BGARefreshLayout.BGARefreshLayoutDelegate {


    @BindView(R.id.lv_lv)
    ListView mListView;

    private int mPage = 1;
    View view;
    @BindView(R.id.layout_no)
    RelativeLayout layoutNo;
    @BindView(R.id.loading_layout)
    AVLoadingIndicatorView loadingLayout;
    @BindView(R.id.layout_no_w)
    RelativeLayout layoutNoW;
    boolean ispaper;
    boolean isjiazai = true;
    ListViewAdapter adapters;
    public static final int DADE_NO_W = 1000;
    public static final int DADE_NO_SJ = 1001;
    public static final int DADE_NO_SXWC = 1002;
    public static final int DADE_NO_SLWC = 1003;
    private BGARefreshLayout mRefreshLayout;
    //自定义的刷新加载view
    BGANormalRefreshViewHolder defineloadview;
    List<MovielistD.DataBean.MovicesBean> result;

    Handler getmHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {

            switch (msg.what) {
                case 1:
                    Log.e("454545", "454545");
                    lazyLoad();
                    break;

                default:
                    break;

            }
            return false;
        }
    });
    String type;

    public static Movie_Fragment newInstance(String type) {

        Bundle args = new Bundle();

        Movie_Fragment fragment = new Movie_Fragment();
        fragment.setArguments(args);

        fragment.type = type;
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (view == null) {
            ispaper = true;
            view = View.inflate(getActivity(), R.layout.fragment_1_myorder__all, null);
            mRefreshLayout = (BGARefreshLayout) view.findViewById(R.id.bga_refresh);
            ButterKnife.bind(this, view);
            EventBus.getDefault().register(this);
            initview();
            lazyLoad();
        }
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null) {
            parent.removeView(view);
        }
        return view;
    }

    // 取消注册
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onEventMainThread(MyEvent event) {

        if (!TextUtils.isEmpty(event.getContent()) && event.getContent().equals("czcwc")) {

            lazyLoad();

        }
    }


    @Override
    public void lazyLoad() {
        if (!isvisible || !ispaper) {
            return;

        }
        if (isjiazai) {

            Firstrequest();
        }

    }

    private void initview() {
        adapters = new ListViewAdapter(getActivity());

        mListView.setAdapter(adapters);
//        mListView.setOnItemClickListener(mbankcardListItemClickListener);
        layoutNoW.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layoutNoW.setVisibility(View.INVISIBLE);
                Firstrequest();
            }
        });
        mRefreshLayout.setDelegate(this);
        defineloadview = new BGANormalRefreshViewHolder(getActivity(), false);
        mRefreshLayout.setRefreshViewHolder(defineloadview);


    }

    @Override
    public void onBGARefreshLayoutBeginRefreshing(BGARefreshLayout refreshLayout) {
        Log.e("11111 ", "111111下拉刷新");
        // 第一页
        mPage = 1;
        // 请求数据
        requestData();
    }

    @Override
    public boolean onBGARefreshLayoutBeginLoadingMore(BGARefreshLayout refreshLayout) {
        Log.e("11111222 ", "11111122上拉加载");
//        defineloadview.showLoadingMoreImg();
//        defineloadview.updateLoadingMoreText("加载中");
        mPage++;
        // 请求数据
        requestData();
        return true;
    }

    //加载网络数据
    private void Firstrequest() {
        LogUtil.log("加载网络数据");
        loadingLayout.setVisibility(View.VISIBLE);
        layoutNo.setVisibility(View.INVISIBLE);
        mRefreshLayout.setVisibility(View.INVISIBLE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
//                if (NetUtil.isNetworkAvailable(BaseApplication.getInstance())) {
                getData();
                //有网
//                } else {
//                    //没网
//                    loadingLayout.setVisibility(View.INVISIBLE);
//                    layoutNoW.setVisibility(View.VISIBLE);
//
//                }
            }
        }, 0);


    }

    public void getData() {


        API.moviceList(type, new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                if (!TextUtils.isEmpty(st)) {
                    LogUtils.e("zzzzz", st);
                    SpfUtils.put(getActivity(), "moviceList" + type, st);

                    MovielistD no_token_message = JSONUtils.parseJSONArray(st, MovielistD.class);

                    if (no_token_message.getCode().equals("200")) {
                        result = no_token_message.getData().getMovices();
//

                        if (result != null && result.size() > 0) {
                            Log.e("111", "111" + "有数据" + mPage);
                            adapters.clear();
                            adapters.addItem(result);
                            mHandler.sendEmptyMessage(DADE_NO_SXWC);
                            loadingLayout.setVisibility(View.INVISIBLE);
                            mRefreshLayout.setVisibility(View.VISIBLE);

                        } else {
                            Log.e("111", "111" + "无数据" + mPage);

                            loadingLayout.setVisibility(View.INVISIBLE);
                            layoutNo.setVisibility(View.VISIBLE);
                            mRefreshLayout.setVisibility(View.INVISIBLE);
                        }


                    }


                }
                isjiazai = false;

            }

            @Override
            public void onFalia(int code, String errst) {
                String o = (String) SpfUtils.get(getActivity(), "moviceList" + type, "");
                if (TextUtils.isEmpty(o)) {
                    loadingLayout.setVisibility(View.INVISIBLE);
                    layoutNoW.setVisibility(View.VISIBLE);
                    isjiazai = true;
                } else {
                    MovielistD no_token_message = JSONUtils.parseJSONArray(o, MovielistD.class);

                    if (no_token_message.getCode().equals("200")) {
                        result = no_token_message.getData().getMovices();


                        if (result != null && result.size() > 0) {
                            Log.e("111", "111" + "有数据" + mPage);
                            adapters.clear();
                            adapters.addItem(result);
                            mHandler.sendEmptyMessage(DADE_NO_SXWC);
                            loadingLayout.setVisibility(View.INVISIBLE);
                            mRefreshLayout.setVisibility(View.VISIBLE);

                        } else {
                            Log.e("111", "111" + "无数据" + mPage);

                            loadingLayout.setVisibility(View.INVISIBLE);
                            layoutNo.setVisibility(View.VISIBLE);
                            mRefreshLayout.setVisibility(View.INVISIBLE);
                        }


                    }


                }
                isjiazai = false;


            }
        });


    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DADE_NO_W:
                    mRefreshLayout.endRefreshing();
                    mRefreshLayout.endLoadingMore();
                    break;
                case DADE_NO_SJ:
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
//                            defineloadview.hideLoadingMoreImg();
//                            defineloadview.updateLoadingMoreText("没有更多数据了");
                            mRefreshLayout.endLoadingMore();

                        }
                    }, 1000);
                    break;
                case DADE_NO_SXWC:

                    mRefreshLayout.endRefreshing();

                    break;
                case DADE_NO_SLWC:
                    mRefreshLayout.endLoadingMore();

                    break;
                default:
                    break;
            }

        }

    };


    private void requestData() {
        // 是否有网
        if (NetUtil.isNetworkAvailable(getActivity())) {
            // 获得数据 通过页数
            LogUtil.log("有网");
            isjiazai = true;
            getData();
        } else {
            // 没网 发消息
            mPage--;
            mHandler.sendEmptyMessage(DADE_NO_W);
        }
    }
}
